package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryCodeByCodeReq;
import com.sg.dto.base.framework.req.QueryCodeDetailReq;
import com.sg.dto.base.framework.req.QueryCodeListReq;
import com.sg.entity.MetadataCode;
import com.sg.service.base.framework.MMetadataCodeService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.MetadataCodeMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MMetadataCodeServiceImpl
        extends ServiceImpl<MetadataCodeMapper, MetadataCode>
        implements MMetadataCodeService {

    @Autowired
    MetadataCodeMapper metadataCodeMapper;

    /**
     * 1-1-13查询编码列表[649]
     * gen by moon at 10/16/2022, 1:01:42 AM
     */
    @Trace(operationName = "1-1-13查询编码列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MetadataCode> queryCodeList(QueryCodeListReq reqDto) {

        try {
            QueryWrapper<MetadataCode> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(MetadataCode.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(MetadataCode.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(MetadataCode.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<MetadataCode>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }


    /**
     * 1-1-13查询编码详情[2181]
     * gen by moon at 10/16/2022, 1:01:42 AM
     */
    @Trace(operationName = "1-1-13查询编码详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MetadataCode queryCodeDetail(QueryCodeDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<MetadataCode> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<MetadataCode> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                MetadataCode retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-13修改编码[90]
     * gen by moon at 10/16/2022, 1:01:41 AM
     */
    @Trace(operationName = "1-1-13修改编码")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCode(MetadataCode metadataCode) {
        metadataCode.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        metadataCode.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (metadataCode != null && metadataCode.getSubjectLifeCycle() != null && metadataCode.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            metadataCode.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (metadataCode != null && metadataCode.getSubjectLifeCycle() != null && metadataCode.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            metadataCode.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        return super.updateById(metadataCode);
    }

    /**
     * 1-1-13新增编码[89]
     * gen by moon at 10/16/2022, 1:01:40 AM
     */
    @Trace(operationName = "1-1-13新增编码")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addCode(MetadataCode metadataCode) {
        metadataCode.setCodeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        metadataCode.setIsArchive("FALSE");
        metadataCode.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        metadataCode.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (metadataCode != null && metadataCode.getSubjectLifeCycle() != null && metadataCode.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            metadataCode.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        metadataCode.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        metadataCode.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (metadataCode != null && metadataCode.getSubjectLifeCycle() != null && metadataCode.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            metadataCode.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        metadataCode.setIsValid("FALSE");
        metadataCode.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        metadataCode.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(metadataCode)) {
            return metadataCode.getCodeId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-1-13批量查询编码byCode[5299]
     * gen by moon at 11/26/2022, 6:14:11 PM
     */
    @Trace(operationName = "1-1-13批量查询编码byCode")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MetadataCode> batchQueryCodeByCode(BatchQueryCodeByCodeReq reqDto) {

        try {
            QueryWrapper<MetadataCode> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getCodeByCodeList() == null || reqDto.getCodeByCodeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("code", reqDto.getCodeByCodeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(MetadataCode.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(MetadataCode.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(MetadataCode.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<MetadataCode>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
